import axios from 'axios'
import qs from 'qs'
import {
	router
} from '@/router';
// import Router from '@/router'
import {
	removeToken,
	getAppId
} from '@/utils/auth'
import {
	Notify,
	Toast
} from 'vant';

let url;
if (process.env.NODE_ENV === "development") {
	url = process.env.VUE_APP_BASE_API
} else {
	url = '/'
}
// create an axios instance
const service = axios.create({
	baseURL: url,
	// baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
	withCredentials: true, // send cookies when cross-domain requests
	timeout: 10000 // request timeout
})

// request interceptor   
service.interceptors.request.use(
	config => {
		config.headers['token'] = localStorage.getItem('usertoken');
		config.headers['language'] = localStorage.getItem('lanage') || '1';
		return config
	},
	error => {
		return Promise.reject(error)
	}
)

// response interceptor
service.interceptors.response.use(
	response => {
		const res = response.data;
		if (res.code != 1) {
			if (res.code == '401') {
				router.push({
					path: "login"
				})
			}
			Toast({
				message: res.msg || 'error',
			})
		
			return Promise.reject(res || 'error');
		} else {
			if (res.msg != 'success' && res.msg != '成功' && res.msg != '获取成功' && res.msg != "" && res.msg != "") {
				Toast({
					message: res.msg,
				})
			}
			if(response.config.url == '/dev-api/api/goods/createOrder' || response.config.url =='/api/goods/createOrder' || response.config.url =='/api/api/goods/createOrder'){
			return res
				
			} else if(response.config.url == '/dev-api/api/goods/payOrder' || response.config.url =='/api/goods/payOrder'  || response.config.url =='/api/api/goods/payOrder' ){
			return res
			}else{
				return res.data
			}
		}
	},
	error => {
		Notify({
			message: error.msg || '错误',
			duration: 3 * 1000,
		});
		return Promise.reject(error)
	}
)
export default service
